package com.gopay.scheduler.job;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.ep.AuthResultEnum;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.cps.transfer.Trans00603VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.domain.cps.vo.FullCpsGenRecvOrder;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import com.gopay.scheduler.dao.ReceiptOrderDAO;
import com.gopay.scheduler.service.cardauth.BankCardAuthService;

/**
 * 银行卡鉴权交易补偿job
 * @BankCardAuthJob.java
 * @author dongdh
 * @2016年5月25日 下午3:45:45  www.gopay.com.cn Inc.All rights reserved.
 */
public class BankCardAuthJob extends GopayBaseJob {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "bankCardAuthService")
	private BankCardAuthService authService;
	
	@Resource(name = "receiptOrderDAO")
	private ReceiptOrderDAO receiptOrderDAO;
	
	@Resource(name = "callCPSHelper")
	private CallCPSHelper cpsHelper;
	
	@Override
	protected void doJobInternal() {
		logger.error("BankCardAuthJob------------start---------");
		logger.error("开始处理银行卡鉴权 异常交易！");

		List<FullCpsGenRecvOrder> orderList = authService.getOrderList(getStartDate(), getEndDate());
		if (orderList != null && orderList.size() > 0) {
			logger.error("查询获取到的银行卡鉴权 异常交易的数量为： " + orderList.size());
			int successCount = 0;
			for (FullCpsGenRecvOrder order : orderList) {
				try {
				    
				    Trans00603VO transferVo = new Trans00603VO();
			        
			        transferVo.setAmt(new BigDecimal("0.00"));
			        transferVo.setCustId(order.getMainRecvCustId());
			        transferVo.setOutAcct(order.getMainRecvAcct());
			        transferVo.setCustType("C");
			        
			        transferVo.setRmk1(order.getGopayOrderId()); //orderID 存入rmk1
			        transferVo.setRmk2("cron call");

			        CPSResponse response = cpsHelper.callByVOTicket(transferVo);
			        if (!StringUtils.equals(response.getRespCode(), ProcCodeConstants.PROC_CODE_100S1000)) {
			            //鉴权交易
			            logger.info("银行卡鉴权交易失败...respCode=" + response.getRespCode());
			            break;
			        }
				    if (StringUtils.equals(AuthResultEnum.FAIL_TEMP.value, order.getAuthenticationResult())) {
				        //更新子订单
				        receiptOrderDAO.updateAuthResult(order.getGopayOrderId(), AuthResultEnum.FAIL.value);
				    } else if (StringUtils.equals(AuthResultEnum.SUCCESS_TEMP.value, order.getAuthenticationResult())) {
				        receiptOrderDAO.updateAuthResult(order.getGopayOrderId(), AuthResultEnum.SUCCESS.value);
				    }
				    successCount++;
				} catch (GopayException e) {
					logger.error("银行卡鉴权 异常交易 补偿,GopayOrderId:" + order.getGopayOrderId());
					logger.error(e.getMessage());
				} catch (Exception e) {
					logger.error("银行卡鉴权 异常交易 补偿,GopayOrderId:" + order.getGopayOrderId());
					logger.error(e.getMessage());
				}
			}
			logger.error("修改成功数： " + successCount);
			logger.error("银行卡鉴权 异常交易 补偿处理结束。");
		}else {
			logger.info("银行卡鉴权 异常交易数据列表为空.");
		}
	}

	private String getEndDate() {
	    
	    return DateUtils.format(new Date(), "yyyyMMdd");
	}
	
	private String getStartDate() {
	    Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, -15);
        Date dd = c.getTime();
        return DateUtils.format(dd, "yyyyMMdd");
	}
 
	public static void main(String[] args) {
        Date d = new Date();
        Calendar c = Calendar.getInstance();
        
        c.add(Calendar.DAY_OF_YEAR, -15);
        
        Date dd = c.getTime();
        System.out.println(DateUtils.format(dd, "yyyyMMdd"));
    }
}

